// const TextList = [
// 	'Item_CutdownSales',
// 	'Item_CutdownOpen',
// 	'Item_CutdownClose',
// 	'Item_CutdownKaCha'
// ];

// const SubTextList = ['Item_CutdownEnd', 'Item_CutdownStart', 'Item_CutdownEnd', 'Item_CutdownEnd'];

export const ITEM_TYPE_ENUM = {
	PRE_ORDER: 'PreOrder',
	CUSTOMER: 'Custom',
	IN_STOCK: 'In Stock',
	CLOSE_OUT: 'CloseOut'
};

export function getProductStatus(detail: Product.Detail, now: Date = new Date()) {
	const offsetTime = detail.openDateTime * 1000 - now.getTime();

	/** @description 判断是否有倒计时活动 */
	const HasTimer = (() => {
		// 下面不成立无法出现倒计时
		if (!detail) return false;
		if (detail.itemType === ITEM_TYPE_ENUM.CLOSE_OUT) return true;
		if (detail.openDateTime <= detail.creatDateTime || offsetTime <= 0) return false;

		if (detail.itemType === ITEM_TYPE_ENUM.CUSTOMER) return true;
		if (detail.itemType === ITEM_TYPE_ENUM.PRE_ORDER) return true;
		if (detail.itemType === ITEM_TYPE_ENUM.IN_STOCK && detail.discount) return true;
		return false;
	})();

	/** @description 活动板块颜色 */
	const ColorType = (() => {
		if (!detail) return '';
		if (detail.itemType === ITEM_TYPE_ENUM.CUSTOMER) return 'primary';
		if (detail.itemType === ITEM_TYPE_ENUM.PRE_ORDER) return 'success';
		if (detail.itemType === ITEM_TYPE_ENUM.IN_STOCK) return 'warning';
		if (detail.itemType === ITEM_TYPE_ENUM.CLOSE_OUT) {
			if (offsetTime <= 0) return 'info';
		}
		return 'danger';
	})();

	/** 
    @description 可续订 
    LOG: 修改前，上架倒计时无法订购
    LOG: 修改后，有效期内预订类型的
  */
	const CanPlaceOrder = (() => {
		if (detail.itemType === ITEM_TYPE_ENUM.IN_STOCK) return true;
		if (detail.itemType === ITEM_TYPE_ENUM.CLOSE_OUT) return true;
		if (!HasTimer && detail.itemType !== ITEM_TYPE_ENUM.PRE_ORDER) return false;
		if (!HasTimer && detail.itemType === ITEM_TYPE_ENUM.PRE_ORDER && detail.max === 0) return false;
		if (HasTimer && detail.itemType === ITEM_TYPE_ENUM.PRE_ORDER && detail.max !== 0) return false;
		return true;
	})();

	const TipIndex = (() => {
		if (detail.itemType === ITEM_TYPE_ENUM.PRE_ORDER && detail.max === 0) return 2;
		if (detail.itemType === ITEM_TYPE_ENUM.IN_STOCK && detail.discount) return 0;
		if (detail.itemType === ITEM_TYPE_ENUM.CUSTOMER) return 2;
		if (detail.itemType === ITEM_TYPE_ENUM.CLOSE_OUT) return 3;
		return 1;
	})();

	const displayText = (() => {
		if (detail.itemType === ITEM_TYPE_ENUM.CLOSE_OUT && offsetTime <= 0) return 'sale has ended';
		return '';
	})();

	return {
		offsetTime,
		HasTimer,
		ColorType,
		CanPlaceOrder,
		TipIndex,
		displayText
	};
}
